package net.hn.hnms.biz.safety.config;

import com.alibaba.druid.filter.Filter;
import com.alibaba.druid.filter.logging.Slf4jLogFilter;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallConfig;
import com.alibaba.druid.wall.WallFilter;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

/**
 * 智呈物联 pan'sx
 * 2022/7/30 17:41
 * 1.0 version
 */
@Getter
@Setter
public class DruidFilterProperties {
    private StatFilterWrapper     stat  = new StatFilterWrapper();
    private Slf4jLogFilterWrapper slf4j = new Slf4jLogFilterWrapper();
    private WallFilterWrapper     wall  = new WallFilterWrapper();
    private final List<Filter> filters = new ArrayList<>();

    @Getter
    @Setter
    public static class StatFilterWrapper extends StatFilter {

        private boolean enabled;
    }

    @Getter
    @Setter
    public static class Slf4jLogFilterWrapper extends Slf4jLogFilter {

        private boolean enabled;
    }

    @Getter
    @Setter
    public static class WallFilterWrapper extends WallFilter {

        private boolean enabled;

        private WallConfig config;

        public WallFilterWrapper() {
            this.setConfig(config);
        }
    }

    public List<Filter> getFilters() {
        if (this.filters.isEmpty()) {
            if (stat.isEnabled()) {
                filters.add(stat);
            }
            if (slf4j.isEnabled()) {
                filters.add(slf4j);
            }
            if (wall.isEnabled()) {
                filters.add(wall);
            }
        }
        return filters;
    }
}
